﻿using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace UserData
{
    public partial class Form_NGControl : UIPage
    {
        string strName = "";
        public Form_NGControl(string strName)
        {
            this.strName = strName;
            InitializeComponent();
        }

        private void Form_NGControl_Load(object sender, EventArgs e)
        {
            PanelNGName.Text = strName;
            foreach (var item in NGCountMgr.GetInstance().NGTatal)
            {
                if (item.Key==strName)
                {
                    NGCountMgr.GetInstance().OnTestReflushNG += OnReslushNGCount;
                    table_NGCount.RowStyles.Clear();
                    table_NGCount.ColumnStyles.Clear();
                    table_NGCount.Controls.Clear();
                    int n_Row = (int)item.Value.Count;
                    int n_Column = 2;
                    table_NGCount.RowCount = n_Row;
                    table_NGCount.ColumnCount = n_Column;
                    for (int i = 0; i < n_Row; i++)
                    {
                        table_NGCount.RowStyles.Add(new RowStyle(SizeType.Percent, (float)1.0 / n_Row));
                        for (int j = 0; j < n_Column; j++)
                        {
                            table_NGCount.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, (float)1.0 / n_Column));
                        }
                    }

                    int n_Index = 0;
                    foreach (var itemValue in item.Value)
                    {
                        UILabel lable_Name = new UILabel();
                        lable_Name.Dock = DockStyle.Fill;
                        lable_Name.BackColor = Color.Red;
                        lable_Name.Text = itemValue.Key;
                        lable_Name.TextAlign = ContentAlignment.MiddleCenter;
                        table_NGCount.Controls.Add(lable_Name, 0, n_Index);
                        UILabel lable_Value = new UILabel();
                        lable_Value.Dock = DockStyle.Fill;
                        lable_Value.BackColor = Color.Red;
                        lable_Value.Text = itemValue.Value.ToString();
                        lable_Value.TextAlign = ContentAlignment.MiddleCenter;
                        table_NGCount.Controls.Add(lable_Value, 1, n_Index);
                        n_Index++;

                    }

                }
            }

        }
        public void OnReslushNGCount()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => OnReslushNGCount()));
            }
            else
            {
                int n_Index = 0;
                foreach (var item in NGCountMgr.GetInstance().NGTatal)
                {
                    if(item.Key==strName)
                    {
                        foreach (var itemValue in item.Value)
                        {
                            ((UILabel)table_NGCount.Controls[n_Index * 2 + 1]).Text = itemValue.Value.ToString();
                            n_Index++;

                        }
                    }
                }
            }


        }

        private void 清除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NGCountMgr.GetInstance().ClearNGCount();
            TestTimeMgr.GetInstance().Function_ClearData();

        }
    }
}
